package com.shenghua.service.impl;

import com.alibaba.fastjson.JSON;
import com.shenghua.dao.model.AppFile;
import com.shenghua.dao.model.User;
import com.shenghua.service.BeanFactoryService;
import com.shenghua.utils.DateUtil;
import com.shenghua.utils.StringUtils;
import org.apache.commons.io.FilenameUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * Bean初始化
 * Created by Administrator on 2019/9/1.
 */
@Service
public class BeanFactoryServiceImpl implements BeanFactoryService {
    private static Logger logger = LoggerFactory.getLogger(BeanFactoryServiceImpl.class);
    @Override
    public AppFile createAppFile(User user, MultipartFile file, String newFileName) {
        AppFile appFile  = new AppFile();
        appFile.setId(StringUtils.getUUID());
        appFile.setFileCreatetime(DateUtil.getCurrentDate());
        appFile.setFileCreateuserid(user.getId().toString());
        appFile.setFileCreateusername(user.getLinkman());
        String filename = file.getOriginalFilename();
        String ext = FilenameUtils.getExtension(filename);
        appFile.setFileExt(ext);
        appFile.setFileName(file.getOriginalFilename());
        String filePath = newFileName;
        appFile.setFilePath(filePath);
        appFile.setFileSize(Long.toString(file.getSize()));
        appFile.setFileSaveType("3");
        appFile.setFileState("");
        appFile.setFileBelongto(""); //未设置归属（新增实体，未保存完毕，一般设置为实体的id）
        logger.info("BeanFactoryServiceImpl createAppFile:{}", JSON.toJSONString(appFile));
        return appFile;
    }
}
